import java.util.ArrayList;
import java.util.List;

/**
 * 797. 所有可能的路径
 */
public class No797 {
    private List<List<Integer>> ans;
    private List<Integer> path;
    private int[][] graph;

    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        this.ans = new ArrayList<>();
        this.path = new ArrayList<>();
        this.graph = graph;
        dfs(0);
        return ans;
    }

    private void dfs(int i) {
        path.add(i);
        if (i == graph.length - 1) ans.add(new ArrayList<>(path));
        for (int j : graph[i]) {
            dfs(j);
            path.remove(path.size() - 1);
        }
    }
}
